package com.ynet.eq.haiqing.module.mqtt;

import com.ynet.eq.haiqing.module.mqtt.impl.datatran.*;
import com.ynet.eq.haiqing.module.mqtt.impl.operator.MqttMessageUnlock;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * @author 胡超雄
 * @date 2021/7/1 16:49
 * @desc mqtt 消息处理的上下文处理类
 */
@Slf4j
public class MqttMessageContext {


    private MqttMessageStrategy mms; //声明一个MqttMessageStrategy对象

    /**
     * 提供是否有策略类的方法
     *
     * @return 返回true说明有 ,返回false 说明没有
     */
    public Boolean hasNoStrategy() {
        return mms == null;
    }

    //通过构造方法，传入操作策略再生产具体的操作策略类, 获取实例,,,这里使用了 简单工厂模式..\
    // 方法名应该叫getInstance  获取实例 ,,更好
    public MqttMessageContext(String operator) {
        switch (operator) {
            case "EditPerson-Ack":
                mms = new MqttMessageEditPerson();
                break;
            case "SearchPerson-Ack":
                mms = new MqttMessageSearchPerson();
                break;
            case "DelPerson-Ack":
                mms = new MqttMessageDelPerson();
                break;
            case "QueryPerson-Ack":
                mms = new MqttMessageQueryPerson();
                break;
            case "SearchPersonList-Ack":
                mms = new MqttMessageSearchPersonList();
                break;
            case "EditPersonsNew-Ack":
                mms = new MqttMessageEditPersonsNew();
                break;
            case "QueryProgress-Ack":
                mms = new MqttMessageQueryProgress();
                break;

            //以下都是操作相关的
            case "Unlock-Ack":
                mms = new MqttMessageUnlock();
                break;
            default:
                break;
        }
    }


    //根据策略的不同，调用不同的类
    public void getResult(String s, MqttMessage message) {
        mms.processMessage(s, message);
    }

}
